package com.xxg.renrenuser.dao;

import com.xxg.renrenuser.dao.bean.CodeTable;
import com.xxg.renrenuser.dao.bean.MyKeyValue;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * 代码表Dao
 *
 * @author xxg
 * @date 2020/11/25
 */
@Repository
public class CodeTableDao {
    private final MongoTemplate mongoTemplate;

    public CodeTableDao(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
        createCollection();
    }

    /**
     * 创建collection
     */
    private void createCollection() {
        String collectionName = "codeTable";
        if (!mongoTemplate.getCollectionNames().contains(collectionName)) {
            mongoTemplate.createCollection(CodeTable.class);
            insert();
        }
    }

    /**
     * 创建代码表
     */
    private void insert() {
        String[] tableNames = {"gender-性别", "accountType-账号类型", "userType-用户身份", "tipOffType-举报类型",
                "tipOffStatus-举报处理状态", "companyIdentificationStatus-企业认证状态", "defaultAddress-是否默认地址",
                "feedbackStatus-问题反馈处理状态", "recipientType-消息接收者类型", "followType-关注类型",
                "likeType-喜欢的内容类型", "noticeType-通知类型", "orderType-订单类型", "benefitStatus-收益状态",
                "expired-记录有效性", "vipExpired-VIP是否到期", "activityType-商城活动类型", "attributeType-属性类型",
                "reasonType-售后原因", "afterSalesStatus-售后状态", "expCode-快递公司", "orderStatus-订单状态",
                "paymentType-支付方式", "tradeStatus-交易状态", "houseType-户型", "decorateType-装修方式", "style-装修风格",
                "caseStatus-案例状态", "caseType-案例类型", "afterSalesType-售后类型", "unFreeExpressArea-不包邮省份地区"};
        List<CodeTable> tables = new ArrayList<>(tableNames.length);
        Date createTime = new Date();
        for (String tableName : tableNames) {
            String[] rows = tableName.split("-");
            CodeTable table = new CodeTable();
            table.setTableName(rows[0]);
            table.setDescription(rows[1]);
            table.setCreateTime(createTime);
            tables.add(table);
        }
        mongoTemplate.insert(tables, CodeTable.class);
    }

    /**
     * 检查代码表是否存在
     *
     * @param tableName 表名
     * @return true-存在，false-不存在
     */
    public boolean hasTable(String tableName) {
        Query query = new Query(where("tableName").is(tableName));
        return mongoTemplate.count(query, CodeTable.class) > 0;
    }

    /**
     * 更新代码表
     *
     * @param tableName 表名
     * @param keyValues 代码值
     */
    public void update(String tableName, List<MyKeyValue> keyValues) {
        Query query = new Query(where("tableName").is(tableName));
        Update update = new Update();
        update.set("updateTime", new Date());
        update.set("keyValues", keyValues);
        mongoTemplate.updateFirst(query, update, CodeTable.class);
    }

    /**
     * 获取所有字典表
     *
     * @return 结果
     */
    public List<CodeTable> findAll() {
        return mongoTemplate.findAll(CodeTable.class);
    }
}
